package com.nickstumpos.android.easyVolume.scheduler.util;

import java.util.Calendar;

import com.nickstumpos.android.easyVolume.scheduler.days.DayOfWeek;
import com.nickstumpos.android.easyVolume.scheduler.dto.SchedulingInfoDto;

public class SchedulingUtil {

	public static long findNextRunTime(SchedulingInfoDto schedule) {
		Calendar now = Calendar.getInstance();
		Calendar scheduledTime = schedule.getScheduledTime();
		scheduledTime.set(Calendar.YEAR, now.get(Calendar.YEAR));
		scheduledTime.set(Calendar.MONTH, now.get(Calendar.MONTH));
		scheduledTime.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));
		Calendar nextRun = Calendar.getInstance();
		nextRun.set(Calendar.HOUR_OF_DAY, scheduledTime.get(Calendar.HOUR_OF_DAY));
		nextRun.set(Calendar.MINUTE, scheduledTime.get(Calendar.MINUTE));
		nextRun.set(Calendar.SECOND, scheduledTime.get(Calendar.SECOND));
		int offset = 0;
		if (schedule.isRepeating()) {
			switch (now.get(Calendar.DAY_OF_WEEK)) {
				case Calendar.SUNDAY:
					if (schedule.getDays().contains(DayOfWeek.SUNDAY)) {
						if (scheduledTime.before(now)) {
							offset = 7;
						}
					} else if (schedule.getDays().contains(DayOfWeek.MONDAY)) {
						offset = 1;
					} else if (schedule.getDays().contains(DayOfWeek.TUESDAY)) {
						offset = 2;
					} else if (schedule.getDays().contains(DayOfWeek.WEDNESDAY)) {
						offset = 3;
					} else if (schedule.getDays().contains(DayOfWeek.THURSDAY)) {
						offset = 4;
					} else if (schedule.getDays().contains(DayOfWeek.FRIDAY)) {
						offset = 5;
					} else if (schedule.getDays().contains(DayOfWeek.SATURDAY)) {
						offset = 6;
					}
					break;
				case Calendar.MONDAY:
					if (schedule.getDays().contains(DayOfWeek.MONDAY)) {
						if (scheduledTime.before(now)) {
							offset = 7;
						}
					} else if (schedule.getDays().contains(DayOfWeek.TUESDAY)) {
						offset = 1;
					} else if (schedule.getDays().contains(DayOfWeek.WEDNESDAY)) {
						offset = 2;
					} else if (schedule.getDays().contains(DayOfWeek.THURSDAY)) {
						offset = 3;
					} else if (schedule.getDays().contains(DayOfWeek.FRIDAY)) {
						offset = 4;
					} else if (schedule.getDays().contains(DayOfWeek.SATURDAY)) {
						offset = 5;
					} else if (schedule.getDays().contains(DayOfWeek.SUNDAY)) {
						offset = 6;
					}
					break;
				case Calendar.TUESDAY:
					if (schedule.getDays().contains(DayOfWeek.TUESDAY)) {
						if (scheduledTime.before(now)) {
							offset = 7;
						}
					} else if (schedule.getDays().contains(DayOfWeek.WEDNESDAY)) {
						offset = 1;
					} else if (schedule.getDays().contains(DayOfWeek.THURSDAY)) {
						offset = 2;
					} else if (schedule.getDays().contains(DayOfWeek.FRIDAY)) {
						offset = 3;
					} else if (schedule.getDays().contains(DayOfWeek.SATURDAY)) {
						offset = 4;
					} else if (schedule.getDays().contains(DayOfWeek.SUNDAY)) {
						offset = 5;
					} else if (schedule.getDays().contains(DayOfWeek.MONDAY)) {
						offset = 6;
					}
					break;
				case Calendar.WEDNESDAY:
					if (schedule.getDays().contains(DayOfWeek.WEDNESDAY)) {
						if (scheduledTime.before(now)) {
							offset = 7;
						}
					} else if (schedule.getDays().contains(DayOfWeek.THURSDAY)) {
						offset = 1;
					} else if (schedule.getDays().contains(DayOfWeek.FRIDAY)) {
						offset = 2;
					} else if (schedule.getDays().contains(DayOfWeek.SATURDAY)) {
						offset = 3;
					} else if (schedule.getDays().contains(DayOfWeek.SUNDAY)) {
						offset = 4;
					} else if (schedule.getDays().contains(DayOfWeek.MONDAY)) {
						offset = 5;
					} else if (schedule.getDays().contains(DayOfWeek.TUESDAY)) {
						offset = 6;
					}
					break;
				case Calendar.THURSDAY:
					if (schedule.getDays().contains(DayOfWeek.THURSDAY)) {
						if (scheduledTime.before(now)) {
							offset = 7;
						}
					} else if (schedule.getDays().contains(DayOfWeek.FRIDAY)) {
						offset = 1;
					} else if (schedule.getDays().contains(DayOfWeek.SATURDAY)) {
						offset = 2;
					} else if (schedule.getDays().contains(DayOfWeek.SUNDAY)) {
						offset = 3;
					} else if (schedule.getDays().contains(DayOfWeek.MONDAY)) {
						offset = 4;
					} else if (schedule.getDays().contains(DayOfWeek.TUESDAY)) {
						offset = 5;
					} else if (schedule.getDays().contains(DayOfWeek.WEDNESDAY)) {
						offset = 6;
					}
					break;
				case Calendar.FRIDAY:
					if (schedule.getDays().contains(DayOfWeek.FRIDAY)) {
						if (scheduledTime.before(now)) {
							offset = 7;
						}
					} else if (schedule.getDays().contains(DayOfWeek.SATURDAY)) {
						offset = 1;
					} else if (schedule.getDays().contains(DayOfWeek.SUNDAY)) {
						offset = 2;
					} else if (schedule.getDays().contains(DayOfWeek.MONDAY)) {
						offset = 3;
					} else if (schedule.getDays().contains(DayOfWeek.TUESDAY)) {
						offset = 4;
					} else if (schedule.getDays().contains(DayOfWeek.WEDNESDAY)) {
						offset = 5;
					} else if (schedule.getDays().contains(DayOfWeek.THURSDAY)) {
						offset = 6;
					}
					break;
				case Calendar.SATURDAY:
					if (schedule.getDays().contains(DayOfWeek.SATURDAY)) {
						if (scheduledTime.before(now)) {
							offset = 7;
						}
					} else if (schedule.getDays().contains(DayOfWeek.SUNDAY)) {
						offset = 1;
					} else if (schedule.getDays().contains(DayOfWeek.MONDAY)) {
						offset = 2;
					} else if (schedule.getDays().contains(DayOfWeek.TUESDAY)) {
						offset = 3;
					} else if (schedule.getDays().contains(DayOfWeek.WEDNESDAY)) {
						offset = 4;
					} else if (schedule.getDays().contains(DayOfWeek.THURSDAY)) {
						offset = 5;
					} else if (schedule.getDays().contains(DayOfWeek.FRIDAY)) {
						offset = 6;

					}
					break;
			}
			nextRun.add(Calendar.DAY_OF_YEAR, offset);
		} else {
			nextRun = schedule.getScheduledTime();
		}
		return nextRun.getTimeInMillis();

	}
}
